package com.bysj.money_diary;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class RegisterShualianActivity extends Activity implements OnClickListener{
	String tag = null;
	private ImageView iv_face;
	private Button ib_face_enter;
	private Button ib_face_choice;
	public Bitmap scalingPhoto;// 位图
	
	private Paint paint;// 画笔工具
	private GifView gif;
	public static boolean flag = false;//用来标志是否为人脸
	public static String picdata;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_shualian);
		Log.i(tag, "start");
		initView();
		
	}
	private void initView() {
		
		iv_face = (ImageView) findViewById(R.id.iv_face);
		
		ib_face_enter = (Button) findViewById(R.id.register_shulian_queding);
		
		ib_face_choice = (Button) findViewById(R.id.register_shulian_submit);
		
		gif = (GifView) findViewById(R.id.gif);
		
		ib_face_enter.setOnClickListener(this);
		ib_face_choice.setOnClickListener(this);
		
		
		
		paint = new Paint();// 创建画笔
//		Log.i(tag, "paint = "+paint);
		scalingPhoto = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.defualt);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.register_shulian_submit:
			// 锟斤拷图锟斤拷
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_PICK);
			intent.setType("image/*");
			startActivityForResult(intent, 1);// 通锟斤拷氐锟斤拷蚩锟斤拷锟�
			break;
		case R.id.register_shulian_queding:
			// 显示加载动画
			gif.setVisibility(View.VISIBLE);
			gif.setMovieResource(R.drawable.red); // 设置背景gif图片资源
			// 确定按钮不允许再次点击

			// 锟斤拷锟饺凤拷锟�
			String base64ImageEncode = ImageUtil
					.getBase64ImageEncode(scalingPhoto);
			System.out.println(base64ImageEncode);
			// 锟斤拷装锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
			final Map<String, Object> map = new HashMap<String, Object>();
			map.put("api_key", Constant.API_KEY);
			map.put("api_secret", Constant.API_SECRET);
			map.put("return_attributes", "gender,age,beauty");
			map.put("image_base64", base64ImageEncode);

			// 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						String result = HttpUtils
								.post(Constant.URL_DETECT, map);
						Message message = new Message();
						message.obj = result;
						handler.sendMessage(message);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}).start();
			break;
		}
		
	}
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			String str = (String) msg.obj;
//			System.out.println("*******face:" + str);
			if (str.equals("403") || str.equals("400") || str.equals("413")
					|| str.equals("500")) {
				
				showMsg("请重新验证");
			} else {
				try {
					JSONObject resultJSON = new JSONObject(str);
					System.out.println(resultJSON.getString("faces") + "=====");
					if (resultJSON.getString("faces").equals("[]")) {
						showMsg("不能识别，请重新输入");
						flag = false;
						
					} else {
						@SuppressWarnings("rawtypes")
						List res = DrawUtil.FacePrepareBitmap(resultJSON,
								scalingPhoto, paint, iv_face);
						flag = true;
						picdata = ImageUtil
								.getBase64ImageEncode(scalingPhoto);
						showMsg("识别成功！请返回继续注册！");
//						face = (String) res.get(3);
//						Log.i(tag, "face = "+face);
//						gender = (String) res.get(0);
//						age = (Integer) res.get(1);
//						beauty = (Integer) res.get(2);
//
//						System.out.println("------------" + gender + " " + age
//								+ " " + " " + beauty);
//
//	6					
//
//						tv_face_gender.setText("性别:"
//								+ ImageUtil.getFaceGender(gender));
//						tv_face_age.setText("年龄:" + age);
//						tv_face_beauty.setText("颜值:" + beauty);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			gif.setVisibility(View.GONE);// 停止gif
		};
	};
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1) {
			// 锟矫伙拷选锟斤拷锟酵计拷锟斤拷锟斤拷锟斤拷荽锟斤拷锟絛ata锟斤拷
			if (data != null) {
				// 锟矫碉拷图片锟斤拷路锟斤拷
				String photoPath = ImageUtil.getPhotoPath(this, data);
				// 锟斤拷锟斤拷
				scalingPhoto = ImageUtil.getScalingPhoto(photoPath);
				// 锟斤拷示图片
				iv_face.setImageBitmap(scalingPhoto);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	private void showMsg(String string) {
		Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
	}
	
	

}
